Express = require 'express'
Config = require './config'
Logger = require('log4js').getLogger()
RedisClient = require './models/redis_client'
ConnectRedis = require('connect-redis') Express
HttpHandler = require './helpers/http_handler'


# app = Express()
app = Express.createServer()

#session store
redisStore = new ConnectRedis
  client : RedisClient


#config
app.configure () ->
  app.set 'view', "#{__dirname}/views"
  app.set 'view engine', 'jade'
  if !Config.debug
    app.use Express.logger()
  app.use HttpHandler.staticHandler
  app.use Express.bodyParser()
  app.use Express.cookieParser()
  app.use Express.session 
    key : Config.session.key
    secret : Config.session.secret
    store : redisStore
  app.use Express.methodOverride()
  app.use HttpHandler.cacheHandler
  app.use app.router
  # app.use gzippo.staticGzip "#{__dirname}/public"

app.configure 'production', () ->
  app.use Express.errorHandler()
  process.on 'uncaughtException', (err) ->
    Logger.error "uncaughtException:#{err}"

app.configure 'development', () ->
  app.use Express.errorHandler
    dumpExceptions : true
    showStack : true


#routes
# require('./apps/vicanso/routes')(app)

#routes
require('./apps/ys/routes')(app)

listenPort = Config.server.port

app.listen listenPort


Logger.info "express linsten on port %d in %s mode", listenPort, app.settings.env


